package yaohua.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

import yaohua.entity.Entity_help;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

/**
 * 数目详细列表信息页面
 * 
 * @author yh
 * 
 */
public class BorrowItemActivity extends ListActivity
{
		private Button renew;
		ArrayList<String> renewUrl;
		int position=-1;
		ProgressDialog progressDialog;
		@Override
		protected void onCreate(Bundle savedInstanceState)
			{
				super.onCreate(savedInstanceState);
				
				// 设置无标题
				requestWindowFeature(Window.FEATURE_NO_TITLE);
				setContentView(R.layout.borrowitem);
				//获取组件
				renew=(Button) findViewById(R.id.renew);
				renew.setOnClickListener(new RenewListener());
				// 从intent中获取数据

				Intent intent = getIntent();
				ArrayList listItem = intent.getParcelableArrayListExtra("listItem");
				renewUrl=intent.getStringArrayListExtra("url");
				position=intent.getIntExtra("position", -1);
				SimpleAdapter sa = new SimpleAdapter(this, listItem, android.R.layout.simple_list_item_2, new String[]
					{ "title", "value" }, new int[]
					{ android.R.id.text1, android.R.id.text2 });
				this.setListAdapter(sa);
				
			}
		/**
		 * 续借按钮监听代码
		 * @author yh
		 *
		 */
		class RenewListener implements OnClickListener
		{
			//handler异步处理
			Handler handler=new Handler(){
				public void handleMessage(Message msg)
					{
						super.handleMessage(msg);
						if (msg.arg1 == -1)
							{
								progressDialog.dismiss();
								Toast.makeText(getApplicationContext(), "网络连接不可用，请检查网络设置", Toast.LENGTH_SHORT).show();
								msg.arg1 = 0;
							}
						else if (msg.arg1 == -2)
							{
								progressDialog.dismiss();
								Toast.makeText(getApplicationContext(), "内部服务器错误", Toast.LENGTH_SHORT).show();
								msg.arg1 = 0;
							}
						
						else if (msg.arg1 == -3)
							{
								progressDialog.dismiss();
								Toast.makeText(getApplicationContext(), "此段时间无法续借", Toast.LENGTH_SHORT).show();
								msg.arg1 = 0;
							}
							
						else if (msg.arg1 == 1)
							{
								progressDialog.dismiss();
								Toast.makeText(getApplicationContext(), "续借成功！", Toast.LENGTH_SHORT).show();
								msg.arg1 = 0;
							}
						else if (msg.arg1 == 2)
							{
								progressDialog.dismiss();
								Toast.makeText(getApplicationContext(), "已到最高续借次数！不能续借！", Toast.LENGTH_SHORT).show();
								msg.arg1 = 0;
							}
						else if(msg.arg1==3)
							{
								progressDialog.dismiss();
								Toast.makeText(getApplicationContext(), "续借失败，请检查是否过期！", Toast.LENGTH_SHORT).show();
								msg.arg1 = 0;
							}
					}
			};

			public void onClick(View arg0)
				{
					// TODO Auto-generated method stub
					progressDialog=ProgressDialog.show(BorrowItemActivity.this, "正在执行续约", "请稍等",false,true);
					new Thread(new Runnable(){

						public void run()
							{
								// TODO Auto-generated method stub
								try
									{
										Entity_help entity_help = new Entity_help();
										//特殊情况下，比如在寒暑假期间，网站不会给不续借链接，所以这里的renewUrl里面为空
										if(renewUrl==null)
											{
												Message msg=new Message();
												msg.arg1=-3;
												handler.sendMessage(msg);
											}
										else
											{
												HttpResponse httpResponse = entity_help.getResponse(renewUrl.get(position));
												
												// 没有网络的话，此处httpResponse==null
												if (httpResponse == null)
													{
														Message msg = new Message();
														msg.arg1 = -1;
														handler.sendMessage(msg);
													}
												// 服务器错误
												
												else if (!entity_help.code200(httpResponse))
													{
														Message msg = new Message();
														msg.arg1 = -2;
														handler.sendMessage(msg);
													}
												
												
												
												else
													{
														
																// 获得返回的的登录提示文字
																HttpEntity entity = entity_help.getEntity(httpResponse);

																String result = entity_help.getContent(entity);

																Pattern regex = Pattern.compile("续借信息", Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNICODE_CASE);
																Matcher regexMatcher = regex.matcher(result);
																boolean foundMatch = regexMatcher.find();
																if (foundMatch)
																	{
																		// 提示续约成功
																		Message msg = new Message();
																		msg.arg1 = 1;
																		handler.sendMessage(msg);
																	}
																else
																	{
																		regex = Pattern.compile("已到最高续借次数！不能续借！", Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNICODE_CASE);
																		regexMatcher = regex.matcher(result);
																		foundMatch = regexMatcher.find();
																		// 提示无法续约，提示原因
																		if(foundMatch)
																			{
																				Message msg = new Message();
																				msg.arg1 = 2;
																				handler.sendMessage(msg);
																			}
																		else
																			{
																				Message msg=new Message();
																				msg.arg1=3;
																				handler.sendMessage(msg);
																			}
																		
																	}
															
													
													}
											}
								
									}
								catch (Exception e)
									{
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								finally
								{
									progressDialog.dismiss();
								}
							}
						
					}).start();
				}
			
			
		}
		@Override
		public boolean onOptionsItemSelected(MenuItem item)
			{
				new AlertDialog.Builder(this).setTitle("关于").setMessage("本程序为中北大学图书馆使用,欢迎您能向作者提供在使用过程中的bug,以方便作者进行修改.联系作者新浪微薄@耀华info")
						.show();
				return super.onOptionsItemSelected(item);
			}

		@Override
		public boolean onCreateOptionsMenu(Menu menu)
			{
				menu.add("关于");
				return super.onCreateOptionsMenu(menu);
			}

	}
